package com.xindacloud.sms.console.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.console.result.Result;
import com.xindacloud.sms.console.result.ResultGenerator;
import com.xindacloud.sms.console.system.dto.SmsRoleDTO;
import com.xindacloud.sms.console.system.entity.SmsMenuRole;
import com.xindacloud.sms.console.system.entity.SmsRole;
import com.xindacloud.sms.console.system.mapper.SmsMenuRoleMapper;
import com.xindacloud.sms.console.system.mapper.SmsRoleMapper;
import com.xindacloud.sms.console.system.service.ISmsRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author czc
 * @since 2019-10-08
 */
@Service
public class SmsRoleServiceImpl extends ServiceImpl<SmsRoleMapper, SmsRole> implements ISmsRoleService {

    @Autowired
    private SmsMenuRoleMapper smsMenuRoleMapper;

    @Override
    public Result getSmsRoleList(SmsRoleDTO smsRoleDTO) {
        IPage<SmsRole> page = new Page<>(smsRoleDTO.getPageNum(), smsRoleDTO.getPageSize());
        QueryWrapper<SmsRole> queryWrapper = new QueryWrapper<>();
        if (smsRoleDTO.getRoleName() != null) {
            queryWrapper.eq("role_name", smsRoleDTO.getRoleName());
        }
        IPage<SmsRole> iPage = this.page(page, queryWrapper);
        List<SmsRoleDTO> dtoList = new ArrayList<>();
        List<SmsRole> srList = iPage.getRecords();
        for (SmsRole role : srList) {
            SmsRoleDTO dto = new SmsRoleDTO();
            dto.setRoleId(role.getRoleId());
            dto.setRoleName(role.getRoleName());
            QueryWrapper<SmsMenuRole> wrapper = new QueryWrapper<>();
            wrapper.eq("role_id", role.getRoleId());
            List<SmsMenuRole> smrList = smsMenuRoleMapper.selectList(wrapper);
            List<Integer> menuIds = new ArrayList<>();
            for (SmsMenuRole smr : smrList) {
                menuIds.add(smr.getMenuId());
            }
            dto.setMenuIds(menuIds);
            dtoList.add(dto);
        }
        IPage<SmsRoleDTO> dtopage = new Page<>();
        dtopage.setCurrent(iPage.getCurrent());
        dtopage.setSize(iPage.getSize());
        dtopage.setRecords(dtoList);
        dtopage.setTotal(iPage.getTotal());
        return ResultGenerator.genSuccessResult(dtopage);
    }

    @Override
    public Result saveOrUpdateSmsRole(SmsRoleDTO smsRoleDTO) {
        SmsRole smsRole = new SmsRole();
        System.out.println(smsRoleDTO.getRoleName()+"--------"+smsRoleDTO.getRolePrivLevel());
        try {
            BeanUtils.copyProperties(smsRoleDTO, smsRole);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(smsRole.getRoleName()+"--------"+smsRole.getRolePrivLevel());
        if (this.saveOrUpdate(smsRole)) {
            if (smsRoleDTO.getRoleId() != null) {
                QueryWrapper<SmsMenuRole> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("role_id", smsRoleDTO.getRoleId());
                smsMenuRoleMapper.delete(queryWrapper);
                SmsMenuRole menuRole = new SmsMenuRole();
                List<Integer> menuIds = smsRoleDTO.getMenuIds();
                for (Integer menuId : menuIds) {
                    menuRole.setRoleId(smsRoleDTO.getRoleId());
                    menuRole.setMenuId(menuId);
                    smsMenuRoleMapper.insert(menuRole);
                }

            } else {

                SmsMenuRole menuRole = new SmsMenuRole();
                QueryWrapper<SmsRole> queryWrapper = new QueryWrapper<>();
                if (smsRoleDTO.getRoleName() != null) {
                    queryWrapper.eq("role_name", smsRoleDTO.getRoleName());
                }

                SmsRole entity = this.getOne(queryWrapper);
                Integer roleId = entity.getRoleId();
                List<Integer> menuIds = smsRoleDTO.getMenuIds();
                for (Integer menuId : menuIds) {
                    menuRole.setRoleId(roleId);
                    menuRole.setMenuId(menuId);
                    smsMenuRoleMapper.insert(menuRole);
                }
            }
            return ResultGenerator.genSuccessResult("保存或修改成功！");
        } else {
            return ResultGenerator.genClientFailResult("保存或修改失败！");
        }
    }

    @Override
    public Result deleteSmsRole(SmsRoleDTO smsRoleDTO) {
        if (this.removeById(smsRoleDTO.getRoleId())) {
            return ResultGenerator.genSuccessResult("删除成功！");
        } else {
            return ResultGenerator.genClientFailResult("删除失败！");
        }
    }

    @Override
    public List<SmsRole> querySmsRoleListByRoleIds(List<Integer> roleIds) {
        return lambdaQuery()
                .in(SmsRole::getRoleId, roleIds)
                .list();
    }
}
